<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable size="small" style="width: 240px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable size="small" style="width: 240px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="身份" prop="role">
                <el-input v-model="queryParams.role" placeholder="请输入身份" clearable size="small" style="width: 240px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="进入时间" prop="enterTime">
                <el-date-picker v-model="queryParams.enterTime" type="date" clearable size="small" style="width: 240px"
                    @keyup.enter="handleQuery" placeholder="请选择时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="时间范围" prop="dateRange">
                <el-date-picker v-model="queryParams.dateRange" size="small" style="width: 240px" value-format="YYYY-MM-DD"
                    type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" size="mini" @click="handleAdd"
                    v-hasRole="['guest']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" size="mini" @click="handleDelete2"
                    v-hasRole="['guest']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" size="mini" @click="handleExport"
                    v-hasRole="['guest2']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <el-table v-loading="loading" :data="recordList" stripe border @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" sortable />
            <el-table-column label="姓名" prop="name" :show-overflow-tooltip="true" width="120" sortable />
            <el-table-column label="手机号" prop="phone" :show-overflow-tooltip="true" width="120" sortable />
            <el-table-column label="身份" prop="identity" width="100" sortable />
            <el-table-column label="进入时间" align="center" prop="enterTime" width="160" sortable />
            <el-table-column label="创建时间" align="center" prop="addTime" width="160" sortable>
                <template #default="scope">
                    <span>{{ parseTime(scope.row.addTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="备注" prop="season" width="150" :show-overflow-tooltip="true" />
            <el-table-column label="最后修改者" prop="modifyUserName" width="140" sortable />
            <el-table-column label="最后修改时间" align="center" prop="modifyTime" width="160" sortable>
                <template #default="scope">
                    <span>{{ parseTime(scope.row.modifyTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="140">
                <template #default="scope">
                    <el-button size="mini" v-if="scope.row.deleteUpdate" type="text" icon="Edit"
                        @click="handleUpdate(scope.row)" v-hasRole="['guest']">修改</el-button>
                    <el-button size="mini" v-if="scope.row.deleteUpdate" type="text" icon="Delete"
                        @click="handleDelete(scope.row)" v-hasRole="['guest']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改访客记录对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="formRef" :model="updateForm" :rules="rules2" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input class="input" v-model="updateForm.name" placeholder="请输入访客姓名" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input class="input" v-model="updateForm.phone" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="身份" prop="identity">
                    <el-input class="input" v-model="updateForm.identity" placeholder="请输入身份" />
                </el-form-item>
                <el-form-item label="进入时间" prop="enterTime">
                    <el-date-picker style="width:30vw;" v-model="updateForm.enterTime" type="datetime" placeholder="请选择时间"
                        editable="false" :shortcuts="shortcuts" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input class="input" v-model="updateForm.season" type="textarea" show-word-limit maxlength="100"
                        :rows="4" placeholder="请输入内容"></el-input>
                </el-form-item>
            </el-form>
            <el-form class="btnGroup">
                <el-button size="large" :loading="updateLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button size="large" @click="cancel">取 消</el-button>
            </el-form>
        </el-dialog>
    </div>
</template>

<script setup name="guestRecordList">
import { ElNotification } from 'element-plus'
import { recordListApi, updateGuestRecodeApi, delGuestRecordApi } from '@/api/guest'

const router = useRouter();
const { proxy } = getCurrentInstance();

const recordList = ref([]);
const open = ref(false);
const loading = ref(true);
const updateLoading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("修改访客记录");
const dateRange = ref([]);

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        role: undefined,
        dateRange: undefined,
    }
});

const { queryParams, form } = toRefs(data);

const checkMobile = (rule, value, cb) => {
    const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
    if (regMobile.test(value)) {
        return cb()
    }
    cb(new Error('请输入正确的手机号'))
}

const formDate = reactive({
    updateForm: {},
    rules2: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }, {
            validator: checkMobile,
            trigger: 'blur'
        }],
        identity: [{ required: true, message: "身份不能为空", trigger: "blur" }],
        enterTime: [{ required: true, message: "进入时间不能为空", trigger: "blur" }],
        season: [{ required: true, message: "原因不能为空", trigger: "blur" }]
    }
})
const { updateForm, rules2 } = toRefs(formDate);

/** 查询我的填写访客列表 */
const getList = () => {
    loading.value = true;
    recordListApi(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
        if (res.code == 200) {
            recordList.value = res.data.vo;
            total.value = res.data.total;
            loading.value = false;
        } else {
            recordList.value = [];
            total.value = 0;
            loading.value = false;
        }
    })
}
getList();
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    dateRange.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}
/** 表格中删除按钮操作 */
function handleDelete(row) {
    proxy.$modal.confirm('是否确认删除姓名为"' + row.name + '"的数据项?').then(function () {
        return delGuestRecordApi(row.id);
    }).then(() => {
        getList();
        ElNotification({
            title: '提示',
            message: '删除成功',
            type: 'success'
        })
    }).catch(() => { });
}
/** 删除按钮操作 */
function handleDelete2() {
    if (ids && ids.value.length > 0) {
        let id = ""
        console.log(ids.value);
        console.log(ids.value.length);
        proxy.$modal.confirm('是否确认删除选中的数据项?').then(function () {
            if (ids && ids.value.length > 0) {
                for (let i = 0; i < ids.value.length; i++) {
                    if (i <= ids.value.length - 2) {
                        id += ids.value[i] + ","
                    } else if (i == ids.value.length - 1) {
                        id += ids.value[i]
                    }
                }
            }
            return delGuestRecordApi(id);
        }).then(() => {
            getList();
            ElNotification({
                title: '提示',
                message: '删除成功',
                type: 'success'
            })
        }).catch(() => { });
    } else {
        proxy.$modal.msgWarning("请选择要删除的数据项");
    }
}
/** 导出按钮操作 */
function handleExport() {
    proxy.download("system/role/export", {
        ...queryParams.value,
    }, `role_${new Date().getTime()}.xlsx`);
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id)
}
/** 更多操作 */
function handleCommand(command, row) {
    switch (command) {
        case "handleDataScope":
            handleDataScope(row);
            break;
        case "handleAuthUser":
            handleAuthUser(row);
            break;
        default:
            break;
    }
}
/** 添加记录 */
function handleAdd() {
    router.push('/guest/add')
}
/** 点击修改 */
function handleUpdate(row) {
    open.value = !open.value
    updateForm.value = row
}

const cancel = () => {
    open.value = false
    proxy.resetForm("formRef");
}
/** 提交修改记录 */
const submitForm = () => {
    updateLoading.value = true
    proxy.$refs["formRef"].validate(valid => {
        if (valid) {
            updateGuestRecodeApi(updateForm.value).then(res => {
                if (res.code == 200) {
                    ElNotification({
                        title: '提示',
                        message: '修改成功',
                        type: 'success'
                    })
                    updateLoading.value = false
                    getList()
                    cancel()
                }
            });
        }
    });
}

</script>